home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / GCC / V2-4-5 / GPPLIBSR00 / cc / igetline < prev    next >
Text File  |  1993-08-04  |  4KB  |  135 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1992 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include "iostream.h"
  19.  
  20. istream& istream::getline(char* buf, int len, char delim)
  21. {
  22.     _gcount = 0;
  23.     if (ipfx1()) {
  24.     streambuf *sb = rdbuf();
  25.     long count = sb->sgetline(buf, len, delim, -1);
  26.     if (count == len-1)
  27.         set(ios::failbit);
  28.     else {
  29.         int ch = sb->sbumpc();
  30.         if (ch == EOF)
  31.         set(ios::failbit|ios::eofbit);
  32.         else if (ch == (unsigned char)delim)
  33.         count++;
  34.         else
  35.         sb->sungetc(); // Leave delimiter unread.
  36.     }
  37.     _gcount = count;
  38.     }
  39.     return *this;
  40. }
  41.  
  42. istream& istream::get(char* buf, int len, char delim)
  43. {
  44.     _gcount = 0;
  45.     if (ipfx1()) {
  46.     streambuf *sbuf = rdbuf();
  47.     long count = sbuf->sgetline(buf, len, delim, -1);
  48.     if (count < 0 || (count == 0 && sbuf->sgetc() == EOF))
  49.         set(ios::failbit|ios::eofbit);
  50.     else
  51.         _gcount = count;
  52.     }
  53.     return *this;
  54. }
  55.  
  56. //    This is part of the iostream library, providing input/output for C++.
  57. //    Copyright (C) 1992 Free Software Foundation.
  58. //
  59. //    This library is free software; you can redistribute it and/or
  60. //    modify it under the terms of the GNU Library General Public
  61. //    License as published by the Free Software Foundation; either
  62. //    version 2 of the License, or (at your option) any later version.
  63. //
  64. //    This library is distributed in the hope that it will be useful,
  65. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  66. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  67. //    Library General Public License for more details.
  68. //
  69. //    You should have received a copy of the GNU Library General Public
  70. //    License along with this library; if not, write to the Free
  71. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  72.  
  73. // from Doug Schmidt
  74.  
  75. #define CHUNK_SIZE 512
  76.  
  77. /* Reads an arbitrarily long input line terminated by a user-specified
  78.    TERMINATOR.  Super-nifty trick using recursion avoids unnecessary calls
  79.    to NEW! */
  80.  
  81. char *_sb_readline (streambuf *sb, long& total, char terminator) 
  82. {
  83.     char buf[CHUNK_SIZE+1];
  84.     char *ptr;
  85.     int ch;
  86.     
  87.     long count = sb->sgetline(buf, CHUNK_SIZE+1, terminator, -1);
  88.     if (count == EOF)
  89.     return NULL;
  90.     ch = sb->sbumpc();
  91.     long old_total = total;
  92.     total += count;
  93.     if (ch != EOF && ch != terminator) {
  94.     total++; // Include ch in total.
  95.     ptr = _sb_readline(sb, total, terminator);
  96.     if (ptr) {
  97.         memcpy(ptr + old_total, buf, count);
  98.         ptr[old_total+count] = ch;
  99.     }
  100.     return ptr;
  101.     }
  102.     
  103.     if (ptr = new char[total+1]) {
  104.     ptr[total] = '\0';
  105.     memcpy(ptr + total - count, buf, count);
  106.     return ptr;
  107.     } 
  108.     else 
  109.     return NULL;
  110. }
  111.  
  112. /* Reads an arbitrarily long input line terminated by TERMINATOR.
  113.    This routine allocates its own memory, so the user should
  114.    only supply the address of a (char *). */
  115.  
  116. istream& istream::gets(char **s, char delim /* = '\n' */)
  117. {
  118.     if (ipfx1()) {
  119.     long size = 0;
  120.     streambuf *sb = rdbuf();
  121.     *s = _sb_readline (sb, size, delim);
  122.     _gcount = *s ? size : 0;
  123.     if (sb->_flags & _S_EOF_SEEN) {
  124.         set(ios::eofbit);
  125.         if (_gcount == 0)
  126.         set(ios::failbit);
  127.     }
  128.     }
  129.     else {
  130.     _gcount = 0;
  131.     *s = NULL;
  132.     }
  133.     return *this;
  134. }
  135.